<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <el-dialog v-model="props.addVisible" :title="modalType === 'edit' ? '修改型材' : '新增型材'" @close="handleVisible('cancel')">
    <el-form ref="formRef" size="small" label-width="120px" :rules="rules" :model="formData">
      <el-form-item label="型材类型" prop="type">
        <el-select @change="typeChange" v-model="formData.type">
          <el-option v-for="item in profileTypes" :label="item.label" :value="item.value" :key="item.label"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-for="item in formItems" :key="item.label" :label="item.label" :prop="item.value">
        <el-select v-if="item.value.includes('yerCode')" filterable v-model.trim="formData[item.value]">
          <el-option v-for="option in layerCodes" :label="option.label" :value="option.value" :key="option.value"></el-option>
        </el-select>
        <el-input v-else v-model.trim="formData[item.value]" placeholder="请输入" clearable :disabled="item.value == 'serialNumber'"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="small" @click="handleVisible('cancel')">取消</el-button>
        <!-- <el-button size="small" type="primary" @click="generatNumberFn()" :disabled="modalType === 'edit'">生成编号</el-button> -->
        <el-button size="small" type="primary" @click="submitAdd"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<!-- 新增型材 -->
<script name="addProfile" setup>
import {
  profileTypes,
  formItemsForCap,
  formItemsForT,
  formItemsForI,
  formItemsForFI,
  formItemsForFz,
  formItemsForFZFF,
  formItemsForFZAFF,
  formItemsForFC,
  formItemsForFCSF,
  formItemsForFL,
  formItemsForFLATI,
  formItemsForFLA,
  formItemsForFG,
  formItemsForFGM,
  formItemsForFGI,
  formItemsForFGATIW,
  formItemsForFGTI,
  formItemsForFGA,
  formItemsForSK
} from '../profileLibrary.js';
import { saveProfile,generatNumber } from '@/api/profileLibrary';
import { getLayerList } from '@/api/layerLibrary';
const formRef = ref(null);
const rules = ref({
  type: [{ required: true, message: '型材类型是必填的', trigger: 'change' }],
  // serialNumber: [{ required: true, message: '型材编号是必填的', trigger: 'blur' }],
  attributeNumber: [{ required: true, message: '型材属性号是必填的', trigger: 'blur' }],
  mat1Id: [{ required: true, message: 'MAT1_ID是必填的', trigger: 'blur' }],
  epLayerCode: [{ required: true, message: 'ep铺层代号是必填的', trigger: 'blur' }],
  alpha: [{ required: true, message: 'alpha(度)是必填的', trigger: 'blur' }],
  d: [{ required: true, message: '直径是必填的', trigger: 'blur' }],
  h: [{ required: true, message: '高度是必填的', trigger: 'blur' }],
  l: [{ required: true, message: '宽度是必填的', trigger: 'blur' }],
  r1: [{ required: true, message: '直径1是必填的', trigger: 'blur' }],
  r2: [{ required: true, message: '直径2是必填的', trigger: 'blur' }]
});
const props = defineProps(['addVisible', 'rowData']);
const emit = defineEmits(['handleEvent']);
const modalType = props?.rowData ? 'edit' : 'add';
const formItems = ref(formItemsForCap);
const hasNumber = ref(false);//是否有编号
const formData = ref({
  type: props?.rowData?.type,
  id: props?.rowData?.id
});
if(modalType === 'edit'){
  hasNumber.value = true;
}
const layerCodes = ref([]);
const typeChange = (event) => {
  formData.value = {
    type: event
  };
  switch (event) {
    case 'CAP': {
      formItems.value = formItemsForCap;
      break;
    }
    case 'T': {
      formItems.value = formItemsForT;
      break;
    }
    case 'I': {
      formItems.value = formItemsForI;
      break;
    }
    case 'FI': {
      formItems.value = formItemsForFI;
      break;
    }
    // case 'FI': {
    //   formItems.value = formItemsForFI;
    //   break;
    // }
    case 'FZ': {
      formItems.value = formItemsForFz;
      break;
    }
    // case 'FZFF': {
    //   formItems.value = formItemsForFZFF;
    //   break;
    // }
    // case 'FZAFF': {
    //   formItems.value = formItemsForFZAFF;
    //   break;
    // }
    case 'FC': {
      formItems.value = formItemsForFC;
      break;
    }
    // case 'FCSF': {
    //   formItems.value = formItemsForFCSF;
    //   break;
    // }
    case 'FL': {
      formItems.value = formItemsForFL;
      break;
    }
    // case 'FLATI': {
    //   formItems.value = formItemsForFLATI;
    //   break;
    // }
    // case 'FLA': {
    //   formItems.value = formItemsForFLA;
    //   break;
    // }
    case 'FG': {
      formItems.value = formItemsForFG;
      break;
    }
    case 'SK': {
      formItems.value = formItemsForSK;
      break;
    }
    // case 'FGM': {
    //   formItems.value = formItemsForFGM;
    //   break;
    // }
    // case 'FGI': {
    //   formItems.value = formItemsForFGI;
    //   break;
    // }
    // case 'FGATIW': {
    //   formItems.value = formItemsForFGATIW;
    //   break;
    // }
    // case 'FGTI': {
    //   formItems.value = formItemsForFGTI;
    //   break;
    // }
    // case 'FGA': {
    //   formItems.value = formItemsForFGA;
    //   break;
    // }
  }
};
typeChange(formData.value.type);
// 修改时，填充表单数据
if (modalType === 'edit') {
  formItems.value.forEach((el) => {
    formData.value[el.value] = props.rowData[el.value];
  });
}
//生成型材编号
const generatNumberFn = () => {
  //调用接口生成编号
  generatNumber(formData.value).then((res) => {
    if (res) {
      formData.value.serialNumber = res.msg;
      hasNumber.value = true;
      saveProfile(formData.value).then((res) => {
      if (res) {
        modalType === 'add'
          ? ElMessage({ showClose: true, message: '新增成功。', type: 'success' })
          : ElMessage({ showClose: true, message: '修改成功。', type: 'success' });
        handleVisible('submit');
      }
    });
    }
  });
  
};
const handleVisible = (type) => {
  if (type === 'cancel') {
    emit('handleEvent');
  } else {
    emit('handleEvent', { type: 'refresh' });
  }
};
const submitAdd = () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      console.log('验证失败');
      return false;
    }
    if (modalType === 'edit') {
      formData.value.id = props.rowData.id;
    }
    generatNumberFn();
  });
};
const queryParams = ref({
  pageNum: 1,
  pageSize: 1000,
  plyCode: '',
  symmetrical: '',
  isAsc: 'descending',
  hasCustomPly:0
});
//查询layerCodes
const queryLayerCodes= () => {
  layerCodes.value = [];
  getLayerList(queryParams.value).then((res) => {
    if (res && res.rows) {
      layerCodes.value = res.rows.map((item) => {
        return { label: item.plyCode, value: item.plyCode };
      });
    }
  });
};
onMounted(() => {
  queryLayerCodes();
});
</script>
